import { _decorator, Component } from 'cc';
import { create, ssclass, sseffect } from '../state-signals';
const { ccclass } = _decorator;

@ccclass('ScoreView')
@ssclass
export class ScoreView extends Component {
    state = create({
        score: 0,
        name: '张三',
        win: false,
    })

    start() {
        // 模拟异步初始化
        this.scheduleOnce(() => {
            this.state.set({
                score: 10,
                name: '李四',
                win: true,
            })
        }, 0.5)
    }

    @sseffect
    effect() {
        if (this.state.win) {
            this.node.getChildByName('win').active = true
        } else {
            this.node.getChildByName('win').active = false
        }
    }

    addScore() {
        Object.assign(this.state, {
            name: '王无',
            score: this.state.score + 1,
        })
    }

    exports() {
        return {
            addScore: this.addScore,
        }
    }
}
